package com.seari.web.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.seari.bean.Emp;
import com.seari.bean.TableSplitResult;
import com.seari.bean.Tablepar;

@Controller
public class EmpController {

	public Map<String, Emp> dataBase = new HashMap<>();
	

	@GetMapping("list")
	public String view() {
		return "list";
	}

	/**
	 * 新增角色
	 */
	@GetMapping("/add")
	public String add() {
		return "add";
	}
	
	@RequestMapping("/addData")
	@ResponseBody
	public Map<String, Object> addData(Emp empInfo) {
		
		dataBase.put(empInfo.getId(), empInfo);
		
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("code", 200);
		res.put("msg", "成功");
		return res;
	}

	/**
	 * 修改角色
	 * 
	 * @param id
	 * @param mmap
	 * @return
	 */
	@RequestMapping("/editData")
	@ResponseBody
	public Map<String, Object> edit(Emp empInfo) {
		
		dataBase.put(empInfo.getId(), empInfo);
		
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("code", 200);
		res.put("msg", "成功");
		return res;
	}


	@RequestMapping("/list")
	@ResponseBody
	public Object getData(Tablepar tablepar) {
		List<Emp> list = new ArrayList<>();

		Set<Entry<String, Emp>> entrySet = dataBase.entrySet();
		Iterator<Entry<String, Emp>> iterator = entrySet.iterator();
		while(iterator.hasNext()) {
			Entry<String, Emp> next = iterator.next();
			list.add(next.getValue());
		}
		
		PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
		PageInfo<Emp> pageInfo = new PageInfo<Emp>(list);
		TableSplitResult<Emp> result = new TableSplitResult<Emp>(pageInfo.getPageNum(), pageInfo.getTotal(),
				pageInfo.getList());
		return result;
	}

	@RequestMapping("/remove")
	@ResponseBody
	public Map<String, Object> delete(@RequestBody List<String> ids) {
		for (String id : ids) {
			dataBase.remove(id);
		}
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("code", 200);
		res.put("msg", "成功");
		return res;
	}
	
	public void initDataBase() {
		Emp emp1 = new Emp();
		emp1.setId("1000001");
		emp1.setName("卞慧慧");
		emp1.setPostInfo("值班站长");
		emp1.setShift("大四班");
		
		Emp emp2 = new Emp();
		emp2.setId("1000002");
		emp2.setName("胡怡");
		emp2.setPostInfo("值班站长");
		emp2.setShift("大四班");
		
		Emp emp3 = new Emp();
		emp3.setId("1000003");
		emp3.setName("卞慧慧");
		emp3.setPostInfo("值班站长");
		emp3.setShift("大四班");
		
		Emp emp4 = new Emp();
		emp4.setId("1000004");
		emp4.setName("张水");
		emp4.setPostInfo("设备值班员");
		emp4.setShift("早晚班");
		
		Emp emp5 = new Emp();
		emp5.setId("1000005");
		emp5.setName("沈文素");
		emp5.setPostInfo("设备值班员");
		emp5.setShift("早晚班");
		
		Emp emp6 = new Emp();
		emp6.setId("1000006");
		emp6.setName("周洁");
		emp6.setPostInfo("行车值班员");
		emp6.setShift("大四班");
		
		Emp emp7 = new Emp();
		emp7.setId("1000007");
		emp7.setName("姚阿");
		emp7.setPostInfo("行车值班员");
		emp7.setShift("大四班");
		
		Emp emp8 = new Emp();
		emp8.setId("1000008");
		emp8.setName("周洁");
		emp8.setPostInfo("巡视岗站务员(机动)");
		emp8.setShift("早中班");
		
		Emp emp9 = new Emp();
		emp9.setId("1000009");
		emp9.setName("董和");
		emp9.setPostInfo("巡视岗站务员(机动)");
		emp9.setShift("早中班");
		
		Emp emp10 = new Emp();
		emp10.setId("1000010");
		emp10.setName("彭莹莹");
		emp10.setPostInfo("站厅巡视岗");
		emp10.setShift("早中班");
		
		Emp emp11 = new Emp();
		emp11.setId("1000011");
		emp11.setName("王晓燕");
		emp11.setPostInfo("站厅巡视岗");
		emp11.setShift("早中班");
		
		Emp emp12 = new Emp();
		emp12.setId("1000012");
		emp12.setName("陆澄亮");
		emp12.setPostInfo("站台岗");
		emp12.setShift("大四班");
		
		Emp emp13 = new Emp();
		emp13.setId("1000013");
		emp13.setName("范春");
		emp13.setPostInfo("站台岗");
		emp13.setShift("大四班");
		
		Emp emp14 = new Emp();
		emp14.setId("1000014");
		emp14.setName("蔡礼");
		emp14.setPostInfo("站务员");
		emp14.setShift("早中班");
		
		Emp emp15 = new Emp();
		emp15.setId("1000015");
		emp15.setName("彭叶飞");
		emp15.setPostInfo("站务员");
		emp15.setShift("早中班");
		
		dataBase.put("1000001", emp1);
		dataBase.put("1000002", emp2);
		dataBase.put("1000003", emp3);
		dataBase.put("1000004", emp4);
		dataBase.put("1000005", emp5);
		dataBase.put("1000006", emp6);
		dataBase.put("1000007", emp7);
//		dataBase.put("1000008", emp8);
//		dataBase.put("1000009", emp9);
//		dataBase.put("10000010", emp10);
//		dataBase.put("10000011", emp11);
//		dataBase.put("10000012", emp12);
//		dataBase.put("10000013", emp13);
//		dataBase.put("10000014", emp14);
//		dataBase.put("10000015", emp15);
		
	}
	
	public List<Emp> getData() {
		List<Emp> list = new ArrayList<>();
		Set<Entry<String, Emp>> entrySet = dataBase.entrySet();
		Iterator<Entry<String, Emp>> iterator = entrySet.iterator();
		while(iterator.hasNext()) {
			Entry<String, Emp> next = iterator.next();
			list.add(next.getValue());
		}
		return list;
	}
	
	
	public static void main(String[] args) {
	}

}
